package com.administrator.scoreManageSys.controller.system;

import com.administrator.scoreManageSys.service.PermissionsMenuService;
import com.administrator.scoreManageSys.service.RoleService;
import com.administrator.scoreManageSys.service.SystemUserAuthenticationService;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 权限菜单管理控制器
 *
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/system/permissions")
public class PermissionsMenuController {

    @Resource
    private PermissionsMenuService permissionsMenuService;

    /**
     * 获取菜单列表
     *
     * @return 返回菜单集合
     */
    @PostMapping(value = "/menuList")
    @RequiresRoles("system")
    @RequiresPermissions(value = "permission:list", logical = Logical.OR)
    public JSONObject menuList(@RequestBody JSONObject data) {
        try {
            return permissionsMenuService.menuList();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取菜单列表 异常");
        }
    }

    /**
     * 获取父级菜单列表
     *
     * @return 返回菜单集合
     */
    @PostMapping(value = "/getParentList")
    @RequiresRoles("system")
    @RequiresPermissions(value = "permission:list", logical = Logical.OR)
    public JSONObject getParentList() {
        try {
            return permissionsMenuService.getParentList();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取父级菜单列表 异常");
        }
    }

    /**
     * 添加菜单
     *
     * @return 添加菜单结果
     */
    @PostMapping(value = "/menuAdd")
    @RequiresRoles("system")
    @RequiresPermissions(value = "permission:add", logical = Logical.OR)
    public JSONObject menuAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            String permission = data.getString("permission");
            String menuName = data.getString("menuName");
            String route = data.getString("route");
            String icon = data.getString("icon");
            Long parent = data.getLong("parent");
            Long serialNumber = data.getLong("serialNumber");
            Long grade = data.getLong("grade");
            Long menuOpend = data.getLong("menuOpend");
            return permissionsMenuService.menuAdd(permission, menuName, route, icon, parent, serialNumber, grade, menuOpend);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加菜单 异常");
        }
    }

    /**
     * 修改菜单
     *
     * @return 修改菜单
     */
    @PostMapping(value = "/menuUpdate")
    @RequiresRoles("system")
    @RequiresPermissions(value = "permission:edit", logical = Logical.OR)
    public JSONObject menuUpdate(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            String permission = data.getString("permission");
            String menuName = data.getString("menuName");
            String route = data.getString("route");
            String icon = data.getString("icon");
            Long parent = data.getLong("parent");
            Long id = data.getLong("id");
            Long serialNumber = data.getLong("serialNumber");
            Long grade = data.getLong("grade");
            Long menuOpend = data.getLong("menuOpend");
            return permissionsMenuService.menuUpdate(id,permission, menuName, route, icon, parent, serialNumber, grade, menuOpend);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改菜单 异常");
        }
    }

    /**
     * 删除菜单
     *
     * @return 删除菜单
     */
    @PostMapping(value = "/menuDelete")
    @RequiresRoles("system")
    @RequiresPermissions(value = "permission:list", logical = Logical.OR)
    public JSONObject menuDelete(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            Long id = data.getLong("id");
            return permissionsMenuService.menuDelete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除菜单 异常");
        }
    }



}
